package com.hyl.icon.controller;

import cn.hutool.json.JSONUtil;
import com.hyl.icon.common.BaseResponse;
import com.hyl.icon.common.ErrorCode;
import com.hyl.icon.common.ResultUtils;
import com.hyl.icon.exception.BusinessException;
import com.hyl.icon.exception.ThrowUtils;
import com.hyl.icon.model.dto.post.PostAddRequest;
import com.hyl.icon.model.dto.template.TemplateAddRequest;
import com.hyl.icon.model.entity.Post;
import com.hyl.icon.model.entity.Template;
import com.hyl.icon.model.entity.User;
import com.hyl.icon.service.IconService;
import com.hyl.icon.service.TemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/template")
@Slf4j
public class TemplateController {

    @Resource
    private TemplateService templateService;

    /**
     * 创建
     *
     * @param templateAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addPost(@RequestBody TemplateAddRequest templateAddRequest, HttpServletRequest request) {
        if (templateAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Template template = new Template();
        BeanUtils.copyProperties(templateAddRequest, template);
        List<String> industries = templateAddRequest.getIndustries();
        if (industries != null) {
            template.setIndustries(JSONUtil.toJsonStr(industries));
        }

        boolean result = templateService.save(template);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(template.getId());
    }
}
